//
//  RBShopApplyPageVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/31.
//

#import "RBShopApplyPageVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "UIViewController+RBAdd.h"
#import "RBPager.h"
#import "RanTool.h"
#import "NSMutableAttributedString+RBAdd.h"
#import <YYText.h>
#import "RBGrowablePhotoContainer.h"
#import "RBShopApplyCatPopVC.h"
#import "RBShopApplyCatResponse.h"
#import "RBZoneChooseVC.h"
#import "RBUploadTool.h"
#import "RBShopApplyAgreeVC.h"

@interface RBShopApplyPageVC ()

@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)RBTextField *shopNameF;
@property(nonatomic, strong)UILabel *roleSaleL;
@property(nonatomic, strong)UILabel *roleProduceL;
@property(nonatomic, assign)NSInteger roleIndex;
@property(nonatomic, strong)RBTextField *catF;
@property(nonatomic, assign) NSInteger catIndex;
@property(nonatomic, strong)RBTextField *zoneF;
@property(nonatomic, copy) NSString *province;
@property(nonatomic, copy) NSString *city;
@property(nonatomic, copy) NSString *zone;
@property(nonatomic, strong)RBTextField *addressF;
@property(nonatomic, strong)RBTextField *nameF;
@property(nonatomic, strong)RBTextField *phoneF;
@property(nonatomic, strong)RBGrowablePhotoContainer *photoContainer;
@property(nonatomic, strong)RBTextField *passwordF;

@property(nonatomic, strong)YYLabel *readL;
@property(nonatomic, assign)BOOL read;

@property(nonatomic, strong) RBShopApplyCatResponse *catResponse;
@property(nonatomic, strong) RBUploadTool *uploadTool;

@end

@implementation RBShopApplyPageVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getCatData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"申请入驻";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    if(self.response.data.status == 2)
    {
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"审核失败,请重新提交" message:self.response.data.fail_reason preferredStyle:UIAlertControllerStyleAlert];
        [vc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:vc animated:YES completion:nil];
    }
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"店铺名称" placeholder:@"请输入" sel:nil];
            self.shopNameF = (RBTextField *)[view viewWithTag:333];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:[self roleView] withHeight:Zoom(70) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"主营类目" placeholder:@"请选择主营类目" sel:@selector(catViewTapped)];
            self.catF = (RBTextField *)[view viewWithTag:333];
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"所在地区" placeholder:@"请选择所在地区" sel:@selector(zoneViewTapped)];
            self.zoneF = (RBTextField *)[view viewWithTag:333];
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"详细地址" placeholder:@"请输入" sel:nil];
            self.addressF = (RBTextField *)[view viewWithTag:333];
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"运营者姓名" placeholder:@"请输入" sel:nil];
            self.nameF = (RBTextField *)[view viewWithTag:333];
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"联系电话" placeholder:@"请输入" sel:nil];
            self.phoneF = (RBTextField *)[view viewWithTag:333];
            self.phoneF.type = RBTextFieldTypePhone;
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view view_addView:({
                UILabel *view = UILabel.new;
                view.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
                    [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:@"荣誉资质(营业执照、医疗许可)"];
                    [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textGrayColor] string:@"　最多上传十张"];
                }];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(19) bottom:0 left:Zoom(13) right:0 centerX:0];
            
            CGFloat photoW = Zoom(350);
            self.photoContainer = [RBGrowablePhotoContainer viewWithMaxNum:10 colNum:3 editable:YES viewWidth:photoW itemH:Zoom(76)];
            [view view_addView:self.photoContainer withHeight:self.photoContainer.viewHeight width:photoW top:Zoom(20) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
            
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = [self itemViewWithTitle:@"店铺密码" placeholder:@"请输入" sel:nil];
            self.passwordF = (RBTextField *)[view viewWithTag:333];
            self.passwordF.type = RBTextFieldTypePasswordSecurity;
            view;
        }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];

        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:0 color:UIColor.whiteColor align:1 text:@"立即申请" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(23)];
            [view rbAddTapGesWithTarget:self action:@selector(confirmBtnTapped)];
            view;
        }) withHeight:Zoom(46) width:Zoom(350) top:Zoom(50) bottom:CGFLOAT_MIN left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        [view view_addView:({
            self.readL = YYLabel.new;
            self.readL;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:Zoom(22) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:kRBSafeBottomHeight left:0 right:0 centerX:0];
    
    self.roleIndex = 0;
    self.catIndex = -1;
    self.read = YES;
}

#pragma mark - Network

- (void)getCatData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:nil path:@"han_store_cate" post:YES log:YES responseClass:RBShopApplyCatResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.catResponse = data;
        [weak_self fillData];
    } failBlock:nil reloadBlock:^{
        [weak_self getCatData];
    }];
}

#pragma mark - Event

- (void)roleBtnTapped: (UITapGestureRecognizer *)ges
{
    self.roleIndex = ges.view.tag;
}

- (void)catViewTapped
{
    __weak typeof(self) weak_self = self;
    NSMutableArray *arrayM = NSMutableArray.array;
    for (RBShopApplyCatModel *model in self.catResponse.data)
    {
        [arrayM addObject:model.cate_name];
    }
    RBShopApplyCatPopVC *vc = [RBShopApplyCatPopVC vcWithTags:arrayM index:self.catIndex] ;
    vc.block = ^(NSInteger index) {
        weak_self.catIndex = index;
        RBShopApplyCatModel *model = self.catResponse.data[index];
        weak_self.catF.text = model.cate_name;
    };
    [self presentVc:vc];
}

- (void)zoneViewTapped
{
    __weak typeof(self) weak_self = self;
    RBZoneChooseVC *vc = RBZoneChooseVC.new;
    [self presentVc:vc];
    vc.block = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull zone) {
        weak_self.province = province;
        weak_self.city = city;
        weak_self.zone = zone;
        weak_self.zoneF.text = [NSString stringWithFormat:@"%@ %@ %@",province,city,zone];
    };
}

- (void)confirmBtnTapped
{
    __weak typeof(self) weak_self = self;
    if(!self.read)
    {
        [self.view showTip:@"请同意并阅读相关协议" completion:nil];
        return;
    }
    if(!self.shopNameF.text.length)
    {
        [self.view showTip:@"请输入店铺名称" completion:nil];
        return;
    }
    if(self.catIndex < 0)
    {
        [self.view showTip:@"请选择主营类目" completion:nil];
        return;
    }
    if(!self.province.length)
    {
        [self.view showTip:@"请选择所在地区" completion:nil];
        return;
    }
    if(!self.addressF.text.length)
    {
        [self.view showTip:@"请输入详细地址" completion:nil];
        return;
    }
    if(!self.nameF.text.length)
    {
        [self.view showTip:@"请输入运营者姓名" completion:nil];
        return;
    }
    if(!self.phoneF.validate)
    {
        [self.view showTip:@"请输入正确联系电话" completion:nil];
        return;
    }
    if(!self.photoContainer.photos.count)
    {
        [self.view showTip:@"请上传荣誉资质" completion:nil];
        return;
    }
    if(self.passwordF.text.length < 6)
    {
        [self.view showTip:@"请输入6位以上店铺密码" completion:nil];
        return;
    }
    [self.uploadTool uploadData:({
        NSMutableArray *arrayM = NSMutableArray.array;
        NSArray *imgs = self.photoContainer.photos;
        for (UIImage *img in imgs)
        {
            RBUploadModel *model = RBUploadModel.new;
            model.source = img;
            [arrayM addObject:model];
        }
        arrayM;
    }) withCompletion:^(NSArray *data) {
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBUploadModel *model in data)
        {
            [arrayM addObject:model.res];
        }
        [weak_self commitWithImgs:arrayM];
    }];
}

- (void)commitWithImgs: (NSArray *)imgs
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"store_name"] = self.shopNameF.text;
        dicM[@"shenfen"] = @(self.roleIndex + 1);
        dicM[@"store_cate"] = ({
            RBShopApplyCatModel *model = self.catResponse.data[self.catIndex];
            model.id;
        });
        dicM[@"province"] = self.province;
        dicM[@"city"] = self.city;
        dicM[@"qu"] = self.zone;
        dicM[@"address"] = self.addressF.text;
        dicM[@"name"] = self.nameF.text;
        dicM[@"mobile"] = self.phoneF.text;
        dicM[@"xuke"] = [imgs componentsJoinedByString:@","];
        dicM[@"pwd"] = self.passwordF.text;
    } path:@"han_store_step1" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
        [weak_self.view showTip:@"信息已提交,请等待审核" completion:^{
            [weak_self.navigationController popViewControllerAnimated:YES];
        }];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Setter

- (void)setRoleIndex:(NSInteger)roleIndex
{
    _roleIndex = roleIndex;
    UIFont *font = [UIFont systemFontOfSize:Zoom(12)];
    UIColor *color = [RBThemeConfig textBlackColor];
    self.roleSaleL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithImage:[UIImage imageNamed:roleIndex == 0? @"shopApplySel": @"shopApplyUn"] imageSize:CGSizeMake(Zoom(15), Zoom(15)) maxFont:font];
        [maker addAttributeStringWithFont:font color:color string:@" 代理商"];
    }];
    self.roleProduceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithImage:[UIImage imageNamed:roleIndex == 1? @"shopApplySel": @"shopApplyUn"] imageSize:CGSizeMake(Zoom(15), Zoom(15)) maxFont:font];
        [maker addAttributeStringWithFont:font color:color string:@" 生产商"];
    }];
}

- (void)setRead:(BOOL)read
{
    _read = read;
    __weak typeof(self) weakSelf = self;
    NSMutableAttributedString *stringM = NSMutableAttributedString.new;
    UIFont *font = [UIFont systemFontOfSize:Zoom(12)];
    [stringM appendContent:[UIImage imageNamed:read? @"addressListDefault": @"shopApplyUn"] lineSpacing:0 contentMode:UIViewContentModeScaleToFill attachmentSize:CGSizeMake(Zoom(11), Zoom(11)) alignToFont:font alignment:YYTextVerticalAlignmentCenter highlighBgColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        weakSelf.read = !weakSelf.read;
    } longPressAction:nil];
    [stringM appendFont:font color:[RBThemeConfig textGrayColor] lineSpacing:0 text:@"　已阅读并同意" highlighBgColor:nil highlightColor:nil tapAction:nil longPressAction:nil];
    [stringM appendFont:font color:[UIColor colorWithHexString:@"#4EA7EC"] lineSpacing:0 text:@"《电子协议》" highlighBgColor:nil highlightColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        RBShopApplyAgreeVC *vc = RBShopApplyAgreeVC.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    } longPressAction:nil];
    self.readL.attributedText = stringM;
}

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(Zoom(10));
            }];
            view;
        });
    }
    return _scrollView;
}



- (UIView *)itemViewWithTitle: (NSString *)title placeholder: (NSString *)placeholder sel: (SEL)sel
{
    UIView *view = UIView.new;
    if (sel)
    {
        [view rbAddTapGesWithTarget:self action:sel];
    }
    [view view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:({
            UIView *view = UIView.new;
            [view view_addView:[RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(15) bottom:0 left:0 right:0 centerX:0];
            [view view_addView:({
                RBTextField *view = RBTextField.new;
                view.font = [UIFont systemFontOfSize:Zoom(12)];
                view.textColor = [RBThemeConfig textBlackColor];
                view.tag = 333;
                view.placeholder = placeholder;
                if (sel)
                {
                    view.userInteractionEnabled = NO;
                }
                view;
            }) withHeight:Zoom(40) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerY:0];
        if (sel)
        {
            [view h_view_addView:[RanTool imgViewWithMode:0 radius:0 imgName:@"mine_arrow_right"] withHeight:Zoom(9) width:Zoom(5) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(13) right:Zoom(13) centerY:YES];
        }
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = [RBThemeConfig lineGrayColor];
        view;
    }) withHeight:1 width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    return view;
}

- (UIView *)roleView
{
    UIView *view = UIView.new;
    [view view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:({
            UIView *view = UIView.new;
            [view view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"入驻身份"];
                [view addVPriority:1];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(15) bottom:0 left:0 right:0 centerX:0];
            [view view_addView:({
                UIView *view = UIView.new;
                [view h_view_addView:({
                    UILabel *view = UILabel.new;
                    self.roleSaleL = view;
                    view.tag = 0;
                    [view rbAddTapGesWithTarget:self action:@selector(roleBtnTapped:)];
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:CGFLOAT_MIN centerY:0];
                [view h_view_addView:({
                    UILabel *view = UILabel.new;
                    self.roleProduceL = view;
                    view.tag = 1;
                    [view rbAddTapGesWithTarget:self action:@selector(roleBtnTapped:)];
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(53) right:CGFLOAT_MIN centerY:0];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerY:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    [view view_addView:({
        UIView *view = UIView.new;
        view.backgroundColor = [RBThemeConfig lineGrayColor];
        view;
    }) withHeight:1 width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    return view;
}

- (RBUploadTool *)uploadTool
{
    if(!_uploadTool)
    {
        self.uploadTool = RBUploadTool.new;
    }
    return _uploadTool;
}

@end
